#pragma region Copyright
/// <copyright>
/// Copyright (c) 2007 Ilia Shramko.
/// Released under the terms of the GNU Lesser General Public License.
/// </copyright>
#pragma endregion

#pragma once

class DirectShowUtility
{
public:

	static HRESULT FilterExists(IFilterGraph* filterGraph, const GUID& filterId, IBaseFilter* exceptFilter = NULL)
	{
		ATLASSERT(filterGraph != NULL);
		if(filterGraph == NULL) return E_INVALIDARG;

		HRESULT hr = S_OK;
		
		CComPtr<IEnumFilters> filters;
		hr = filterGraph->EnumFilters(&filters);
		if(FAILED(hr)) return hr;

		CComPtr<IUnknown> exceptUnknown;
		if(exceptFilter != NULL)
			hr = exceptFilter->QueryInterface(&exceptUnknown);
		if(FAILED(hr)) return hr;

		bool found = false;
		ULONG n;
		CComPtr<IBaseFilter> filter;
		while(filters->Next(1, &filter, &n) == S_OK)
		{
			CLSID nextFilterId;
			hr = filter->GetClassID(&nextFilterId);
			if(FAILED(hr)) break;

			if(nextFilterId == filterId)
			{
				if(exceptUnknown == NULL)
				{
					found = true;
					break;
				}

				CComPtr<IUnknown> nextFilterUnknown;
				hr = filter->QueryInterface(&nextFilterUnknown);
				if(FAILED(hr)) break;

				if(nextFilterUnknown != exceptUnknown)
				{
					found = true;
					break;
				}
			}
			filter.Release();
			filter.Detach();
		}

		if(FAILED(hr)) return hr;
		return found ? S_OK : S_FALSE;
	}

};








